#ifdef _CH_
#pragma package <opencv>
#endif


#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <math.h>
#include "srav.h"

#include <iostream>
using namespace std;
#define FALSE 0
#define TRUE 1

// RGB, YCbCr, HSV e CIElab!!.

void on_trackbar(int pos)
{
}




int main (int argc, char **argv)
{
  IplImage* src;
  IplImage* Y;   // Y luma component
  IplImage* Cr;  // Cr red-diference chroma component
  IplImage* Cb;  // Cb blue-diference chroma component

  IplImage* Ytmp;
  IplImage* Crtmp;
  IplImage* Cbtmp;
  IplImage* dst;

   const char *original = "Original (YCbCr)";

   const char *Y1 = "luma";
   const char *CR = "red-diference";
   const char *CB = "blue-diference";
   const char *win_mul = "Multiplication";


   int Yminthreshold  = 32;
   int Ymaxthreshold  = 255;
   int CBminthreshold = 0;
   int CBmaxthreshold = 132;
   int CRminthreshold = 144;
   int CRmaxthreshold = 174;

   IplConvKernel* the_elem;

   CvCapture* capture = 0;
   if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
     capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
   else if( argc == 2 )
      capture = cvCaptureFromAVI( argv[1] );

    if( !capture )
      {
        fprintf(stderr,"Could not initialize capturing...\n");
        return -1;
      }
    //
    // Lê arquivo de entrada.

    src = cvQueryFrame( capture );


    CvSize size = cvGetSize(src);

    dst = cvCreateImage(size, 8, 1 );

    Y = cvCreateImage(size, 8, 1 );
    Cb = cvCreateImage(size, 8, 1 );
    Cr = cvCreateImage(size, 8, 1 );

      Ytmp = cvCreateImage(size, 8, 1 );
      Cbtmp = cvCreateImage(size, 8, 1 );
      Crtmp = cvCreateImage(size, 8, 1 );

      dst = cvCreateImage(size, 8, 1);

      //  cvNamedWindow(Y1, 2);
      //  cvNamedWindow(CB, 2);
      //  cvNamedWindow(CR, 2);
      cvNamedWindow("MOR out", 1);
      cvNamedWindow("CAM", 1);
      cvNamedWindow("MUL out", 1);

      cvCreateTrackbar("Luma min", "CAM", &Yminthreshold, 255, on_trackbar);
      cvCreateTrackbar("Luma max", "CAM", &Ymaxthreshold, 255, on_trackbar);
      cvCreateTrackbar("Cb min", "CAM", &CBminthreshold, 255, on_trackbar);
      cvCreateTrackbar("Cb max", "CAM", &CBmaxthreshold, 255, on_trackbar);
      cvCreateTrackbar("Cr min", "CAM", &CRminthreshold, 255, on_trackbar);
      cvCreateTrackbar("Cr max", "CAM", &CRmaxthreshold, 255, on_trackbar);
      preprocess_threshold(src, Y, Ytmp, Cb, Cbtmp, Cr, Crtmp);
        //  cvShowImage(Y1, Ytmp);
        //  cvShowImage(CB, Cbtmp);
        //  cvShowImage(CR, Crtmp);


        for (;;) {

          src = cvQueryFrame( capture );

          cvShowImage("CAM", src);

          if (!src)
            break;

          preprocess_threshold(src, Y,Ytmp, Cb, Cbtmp, Cr, Crtmp);
          skin_threshold(src, dst, the_elem, Y, Ytmp, Ymaxthreshold, Yminthreshold, Cb, Cbtmp, CBmaxthreshold, CBminthreshold, Cr, Crtmp, CRmaxthreshold, CRminthreshold, DST_MUL);
             cvShowImage("MUL out", dst);
             skin_threshold(src, dst, the_elem, Y, Ytmp, Ymaxthreshold, Yminthreshold, Cb, Cbtmp, CBmaxthreshold, CBminthreshold, Cr, Crtmp, CRmaxthreshold, CRminthreshold, DST_MOR);
             cvShowImage("MOR out", dst);
             //    cvShowImage(Y1, Ytmp);
             //cvShowImage(CB, Cbtmp);
             //cvShowImage(CR, Crtmp);
             //    cvShowImage("a", dst);

             int c = cvWaitKey (10);
                if ((char) c == 27)
                  break;
           }
        //cvWaitKey(0);

        cvDestroyAllWindows();

        cvReleaseStructuringElement(&the_elem);

        cvReleaseCapture( &capture );

        /*  cvReleaseImage(&src);*/

        cvReleaseImage(&Y);
        cvReleaseImage(&Cb);
        cvReleaseImage(&Cr);
        cvReleaseImage(&Ytmp);
        cvReleaseImage(&Crtmp);
        cvReleaseImage(&Cbtmp);
        cvReleaseImage(&dst);
        //  cvRel  PRECISO ARRUMAR as dealocações (??)

        return 0;

      }
      /* IplImage* src; */
